#include "chen.h"

int
Socket(int family, int type, int protocol )
{
	int n;
	if ( ( n = socket( family, type, protocol ) ) < 0  )
		err_sys( "socket error" );
	return n;
}

int
Connect( int sockfd, const struct sockaddr *servaddr, socklen_t addrlen )
{
	int n;
	if ( ( n = connect( sockfd, servaddr, addrlen  ) ) < 0 )
		err_sys( "connect error" );
	return n;
}

int
Bind( int sockfd, const struct sockaddr *myaddr, socklen_t addrlen )
{
	int n;
	if ( ( n = bind( sockfd, myaddr, addrlen ) ) < 0 )
		err_sys( "bind error" );
	return n;
}

int
Listen( int sockfd, int backlog )
{
	int n;
	if ( ( n = listen( sockfd, backlog ) ) < 0 )
		err_sys( "listen error" );
	return n;
}

int
Accept( int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen )
{
	int n;
	if ( ( n = accept( sockfd, cliaddr, addrlen ) ) < 0 )
		err_sys( "accept error" );
	return n;
}

size_t
Recvfrom( int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen)
{
	size_t n;
	if ( ( n = recvfrom( sockfd, buff, nbytes, flags, from, addrlen ) ) < 0 )
		err_sys( "recvfrom error" );
	return n;
}

void
Sendto( int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen)
{
	if ( sendto( sockfd, buff, nbytes, flags, to ,addrlen ) != ( ssize_t )nbytes )
		err_sys( "sendto error" );
}

